package hyit.app.servlet;

import hyit.app.factory.DAOFactory;
import hyit.app.model.TeacherInfo;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login2Servlet
 */
@WebServlet("/login2")
public class Login2Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login2Servlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String type = request.getParameter("type");
		List<TeacherInfo> teacherList = null;
		try {
			teacherList = DAOFactory.getITeacherInfoDAOInstance().validate(
					type, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		request.getSession().setAttribute("list", teacherList);
		if (teacherList.size() == 1) {
			request.setAttribute("id", teacherList.get(0).getTeacherNumber());
			request.getRequestDispatcher("login").forward(request, response);
		} else if (teacherList.size() == 0) {
			request.getRequestDispatcher("login.html").forward(request,
					response);
		} else {
			request.getRequestDispatcher("forward.jsp").forward(request,
					response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
